<?php

namespace app\api\model;

use think\Model;

class Theme extends Base
{
    protected $hidden = ['topic_img_id', 'head_img_id', 'delete_time', 'update_time'];
    /**
     * 一对一关系，获取主题对应的图片
     * @return \think\model\relation\BelongsTo
     */
    public function getTopicImg()
    {
        return self::belongsTo('Image', 'topic_img_id', 'id');
    }

    /**
     * 一对一关系，获取主题对应的图片
     * @return \think\model\relation\BelongsTo
     */
    public function getHeadImg()
    {
        return self::belongsTo('Image', 'head_img_id', 'id');
    }

    /**
     *
     * @return \think\model\relation\BelongsToMany
     */
    public function products()
    {
        return self::belongsToMany('Product', 'theme_product', 'product_id', 'theme_id');
    }

    /**
     * 获取首页主题列表
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getThemeList()
    {
        return self::with(['getTopicImg', 'getHeadImg'])->select();
    }

    /**
     * 获取主题详情的内容列表
     *
     * @param $themeId
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getThemeDetail($themeId)
    {
        return self::with(['getTopicImg', 'getHeadImg', 'products'])->find($themeId);
    }
}
